var request = require("request");
var assert = require("assert");
var expect = require("chai").expect;
var should = require("chai").should();
var underscore = require("underscore");

var config = require("../config/index");

describe('engine', function () {

    var test_host = 'http://172.22.200.35';
    var test_port = 8000;
    var params = {
        "serviceType": "twitter",
        "serviceRealm": "global",
        "serviceVersion": 0,
        "op": "get",
        "paramSet": {
            "data": ''
        }
    };
    var test_url = test_host + ':' + test_port;

    function set_options(url_extension, params) {
        if (params != null) {
            var url = test_url + url_extension + JSON.stringify(params);
        }
        else {
            var url = test_url + url_extension;
        }
        var options = {
            url: url,
            headers: {
                'User-Agent': 'request'
            }
        };
        return options;
    }

    describe('twitter#get', function () {

        it('should be found specify message in response', function (done) {

            params["paramSet"]["data"] = "Washington";
            console.log(params);

            var options = set_options('/api?m=', params);

            request(options, function (error, response, body) {
                if (error) throw error;

                console.log(error);

                var res = JSON.parse(body);

                console.log(res);

                assert.equal(response.statusCode, 200);

                res.should.have.messageNode;
                done();
            });

        });

        it('should be return "There is no data for request", in case unknown data', function (done) {

            var messageNode = "18989"
            params['paramSet']['data'] = messageNode;

            console.log(params);

            var options = set_options('/api?m=', params);

            request(options, function (error, response, body) {
                if (error) throw error;

                console.log(error);

                var res = JSON.parse(body);

                console.log(res);

                var expectedErrorMessage = "There is no data for request: " + messageNode + "";

                var actualErrorDetails = res.errorSet[0].details;

                assert.equal(expectedErrorMessage, actualErrorDetails);
                done();
            });
        });

        it('should be return data', function (done) {

            params["paramSet"]["data"] = '';
            console.log(params);

            var options = set_options('/api?m=', params);

            request(options, function (error, response, body) {
                if (error) throw error;

                console.log(error);

                var res = JSON.parse(body);

                console.log(res);

                var resMessage = "virginia";

                var actualResult = res.resultSet;

                actualResult.forEach(function (item) {
                    if (item.text === resMessage) {
                        done();
                    }

                });
            });

        });
        
        it('should be return service status', function (done) {
            params = {
                "serviceType": "twitter",
                "serviceRealm": "global",
                "serviceVersion": 0,
                "op": "serviceStatus"
            };

            var options = set_options('/api?m=', params);

            request(options, function (error, response, body) {
                if (error) throw error;

                console.log(error);

                var res = JSON.parse(body);

                console.log(res);

                var actualResult = res.resultSet.status.code;

                console.log(actualResult);

                assert.equal("ok", actualResult);

                done();
                });
            });

        it('should save without error 2', function (done) {

            var options = set_options('/acs.html', null);

            request(options, function (error, response) {
                if (error) throw error;
                if (response.statusCode == 201) {
                    done();
                }
                else {
                    throw new Error("status not 201");
                    done();
                }
            });
        });
    });
});
